<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>税务PDF数据提取系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            padding-top: 2rem;
            padding-bottom: 2rem;
            background-color: #f8f9fa;
        }
        .header {
            padding-bottom: 1rem;
            margin-bottom: 2rem;
            border-bottom: 1px solid #e5e5e5;
        }
        .footer {
            padding-top: 1.5rem;
            color: #777;
            border-top: 1px solid #e5e5e5;
            margin-top: 2rem;
        }
        .table-container {
            overflow-x: auto;
            margin-top: 1rem;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 2rem 0;
        }
        .spinner-border {
            width: 3rem;
            height: 3rem;
        }
        .result-section {
            display: none;
            margin-top: 2rem;
        }
        .nav-tabs {
            margin-bottom: 1rem;
        }
        pre {
            background-color: #f8f9fa;
            padding: 1rem;
            border-radius: 0.25rem;
            max-height: 400px;
            overflow-y: auto;
        }
        .table-preview {
            margin-bottom: 2rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>税务PDF数据提取系统</h1>
            <p class="lead">上传税务PDF文件，自动提取表格数据并进行规则匹配</p>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5>上传PDF文件</h5>
                    </div>
                    <div class="card-body">
                        <form id="upload-form">
                            <div class="mb-3">
                                <label for="pdf-file" class="form-label">选择PDF文件</label>
                                <input class="form-control" type="file" id="pdf-file" accept=".pdf" required>
                            </div>
                            <div class="mb-3 form-check">
                                <input type="checkbox" class="form-check-input" id="detect-titles" checked>
                                <label class="form-check-label" for="detect-titles">检测表格标题</label>
                            </div>
                            <div class="mb-3 form-check">
                                <input type="checkbox" class="form-check-input" id="merge-tables" checked>
                                <label class="form-check-label" for="merge-tables">合并跨页表格</label>
                            </div>
                            <div class="mb-3 form-check">
                                <input type="checkbox" class="form-check-input" id="apply-rules" checked>
                                <label class="form-check-label" for="apply-rules">应用规则匹配</label>
                            </div>
                            <button type="submit" class="btn btn-primary">提取数据</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="loading" id="loading">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">加载中...</span>
            </div>
            <p class="mt-2">正在处理PDF文件，请稍候...</p>
        </div>

        <div class="result-section" id="result-section">
            <h3>提取结果</h3>
            <ul class="nav nav-tabs" id="result-tabs" role="tablist">
                <li class="nav-item" role="presentation">
                    <button class="nav-link active" id="tables-tab" data-bs-toggle="tab" data-bs-target="#tables" type="button" role="tab" aria-controls="tables" aria-selected="true">表格预览</button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="processed-tab" data-bs-toggle="tab" data-bs-target="#processed" type="button" role="tab" aria-controls="processed" aria-selected="false">处理结果</button>
                </li>
                <li class="nav-item" role="presentation">
                    <button class="nav-link" id="json-tab" data-bs-toggle="tab" data-bs-target="#json" type="button" role="tab" aria-controls="json" aria-selected="false">JSON数据</button>
                </li>
            </ul>
            <div class="tab-content" id="result-tabs-content">
                <div class="tab-pane fade show active" id="tables" role="tabpanel" aria-labelledby="tables-tab">
                    <div id="tables-container"></div>
                </div>
                <div class="tab-pane fade" id="processed" role="tabpanel" aria-labelledby="processed-tab">
                    <div id="processed-container"></div>
                </div>
                <div class="tab-pane fade" id="json" role="tabpanel" aria-labelledby="json-tab">
                    <pre id="json-container"></pre>
                </div>
            </div>
        </div>

        <div class="footer">
            <p>&copy; 2023 税务PDF数据提取系统</p>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const uploadForm = document.getElementById('upload-form');
            const loading = document.getElementById('loading');
            const resultSection = document.getElementById('result-section');
            const tablesContainer = document.getElementById('tables-container');
            const processedContainer = document.getElementById('processed-container');
            const jsonContainer = document.getElementById('json-container');

            uploadForm.addEventListener('submit', function(e) {
                e.preventDefault();
                
                const fileInput = document.getElementById('pdf-file');
                const detectTitles = document.getElementById('detect-titles').checked;
                const mergeTables = document.getElementById('merge-tables').checked;
                const applyRules = document.getElementById('apply-rules').checked;
                
                if (!fileInput.files[0]) {
                    alert('请选择PDF文件');
                    return;
                }
                
                // 显示加载中
                loading.style.display = 'block';
                resultSection.style.display = 'none';
                
                // 创建FormData
                const formData = new FormData();
                formData.append('file', fileInput.files[0]);
                
                // 构建URL
                let url = '/api/pdf/extract?';
                url += `detect_titles=${detectTitles}&`;
                url += `merge_tables=${mergeTables}&`;
                url += `apply_rules=${applyRules}`;
                
                // 发送请求
                fetch(url, {
                    method: 'POST',
                    body: formData
                })
                .then(response => {
                    if (!response.ok) {
                        return response.json().then(err => { throw new Error(err.detail || '请求失败'); });
                    }
                    return response.json();
                })
                .then(data => {
                    // 隐藏加载中
                    loading.style.display = 'none';
                    resultSection.style.display = 'block';
                    
                    // 显示JSON数据
                    jsonContainer.textContent = JSON.stringify(data, null, 2);
                    
                    // 显示表格预览
                    renderTables(data.tables);
                    
                    // 显示处理结果
                    if (data.processed_tables) {
                        renderProcessedTables(data.processed_tables);
                    } else {
                        processedContainer.innerHTML = '<div class="alert alert-info">未应用规则处理</div>';
                    }
                })
                .catch(error => {
                    loading.style.display = 'none';
                    alert('错误: ' + error.message);
                });
            });
            
            function renderTables(tables) {
                if (!tables || tables.length === 0) {
                    tablesContainer.innerHTML = '<div class="alert alert-warning">未检测到表格</div>';
                    return;
                }
                
                let html = '';
                
                tables.forEach((table, index) => {
                    html += `
                        <div class="table-preview">
                            <h4>${table.title || `表格 ${index + 1}`}</h4>
                            <p>页码: ${table.page} | 行数: ${table.rows} | 列数: ${table.cols}</p>
                            <div class="table-container">
                                <table class="table table-bordered table-striped">
                    `;
                    
                    // 添加表格数据
                    if (table.data && table.data.length > 0) {
                        table.data.forEach((row, rowIndex) => {
                            html += '<tr>';
                            row.forEach(cell => {
                                if (rowIndex === 0) {
                                    html += `<th>${cell || ''}</th>`;
                                } else {
                                    html += `<td>${cell || ''}</td>`;
                                }
                            });
                            html += '</tr>';
                        });
                    }
                    
                    html += `
                                </table>
                            </div>
                        </div>
                    `;
                });
                
                tablesContainer.innerHTML = html;
            }
            
            function renderProcessedTables(processedTables) {
                if (!processedTables || processedTables.length === 0) {
                    processedContainer.innerHTML = '<div class="alert alert-warning">未匹配到规则</div>';
                    return;
                }
                
                let html = '';
                
                processedTables.forEach((table, index) => {
                    html += `
                        <div class="card mb-4">
                            <div class="card-header">
                                <h5>${table.rule_name || `规则 ${index + 1}`}</h5>
                                <p class="mb-0">类型: ${table.data_type || '未知'}</p>
                            </div>
                            <div class="card-body">
                                <pre>${JSON.stringify(table.processed_data, null, 2)}</pre>
                            </div>
                        </div>
                    `;
                });
                
                processedContainer.innerHTML = html;
            }
        });
    </script>
</body>
</html>